package com.xiaobai.netease_news.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.views.CircleProgressBar;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class NewsDetailActivity extends Activity
        implements View.OnClickListener {

    private TextView newsContent;
    private CircleProgressBar progressBar;
    private LinearLayout newsDetailCommentLayout;
    private TextView newsTitle;
    private TextView newsSource;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        intent = getIntent();

        newsDetailCommentLayout = (LinearLayout) findViewById(R.id.news_detail_comment_layout);

        progressBar = (CircleProgressBar) findViewById(R.id.news_detail_progress);

        RelativeLayout newsDetailHead = (RelativeLayout) findViewById(R.id.news_detail_head);
        ImageView back = (ImageView) newsDetailHead.findViewById(R.id.news_detail_back);
        TextView replycount = (TextView) newsDetailHead.findViewById(R.id.news_detail_reply_count);
        ImageView more = (ImageView) newsDetailHead.findViewById(R.id.news_detail_more);
        back.setOnClickListener(this);
        replycount.setOnClickListener(this);
        more.setOnClickListener(this);

        replycount.setText(intent.getIntExtra("replyCount", 0) + "跟帖");

        newsTitle = (TextView) findViewById(R.id.news_detail_title);
        newsSource = (TextView) findViewById(R.id.news_detail_source);
        newsContent = (TextView) findViewById(R.id.news_detail_content);

        new NewsDetailTask().execute(intent.getStringExtra("url_3w"));

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.news_detail_back:
                finish();
                break;
            case R.id.news_detail_reply_count:
                break;
            case R.id.news_detail_more:
                break;
        }
    }

    private class NewsDetailTask extends AsyncTask<String, Void, Map<String, Object>> {

        @Override
        protected Map<String, Object> doInBackground(String... params) {
            String content;

            Map<String, Object> map = null;

            try {
                Document document = Jsoup.connect(params[0]).get();

                content = document.select("p").toString();

                map = new HashMap<>();
                map.put("content", content);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return map;
        }

        @Override
        protected void onPostExecute(Map<String, Object> stringObjectMap) {
            super.onPostExecute(stringObjectMap);

            progressBar.setVisibility(View.GONE);
            newsDetailCommentLayout.setVisibility(View.VISIBLE);

            String content = (String) stringObjectMap.get("content");

            newsContent.setMovementMethod(LinkMovementMethod.getInstance());
            newsContent.setText(Html.fromHtml(content));
            newsTitle.setText(intent.getStringExtra("title"));
            newsSource.setText(intent.getStringExtra("source") + "  " + intent.getStringExtra("ptime"));
        }
    }
}
